/*
 * @Author: hanyuanjiao
 * @Date: 2025-07-23 21:20:16
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-08-19 14:07:10
 * @Description: 请填写简介
 */
#ifndef MEM_H
#define MEM_H
#include <stdint.h>

// SFR区中可位寻址的起始地址
#define MEM_BIT_SFR_START 0x80
// IRAM中可位寻址的起始地址
#define MEM_BIT_IRAM_START 0x20

// SFR bit accessible
#define SFR_B     0xF0    // mul中存放乘数,运算结果放高8位;div中放除数,运算后放余数
#define SFR_ACC   0xE0
#define SFR_PSW   0xD0
#define SFR_T2CON 0xC8
#define SFR_IP    0xB8
#define SFR_P3    0xB0
#define SFR_IE    0xA8
#define SFR_P2    0xA0
#define SFR_SCON  0x98
#define SFR_P1    0x90
#define SFR_TCON  0x88
#define SFR_P0    0x80
// SFR bit not accessible
#define SFR_SP     0x81
#define SFR_DPL    0x82
#define SFR_DPH    0x83
#define SFR_PCON   0x87
#define SFR_TMOD   0x89
#define SFR_TL0    0x8a
#define SFR_TL1    0x8b
#define SFR_TH0    0x8c
#define SFR_TH1    0x8d
#define SFR_AUXR   0x8e    //定时器/串口时钟控制器,扩展ram访问
#define SFR_CKCON  0x8F    // cpu/emif总线时序优化
#define SFR_SBUF   0x99
#define SFR_RCAP2L 0xca    //定时器2重载值
#define SFR_RCAP2H 0xcb    //定时器2重载值
#define SFR_TL2    0xcd
#define SFR_TH2    0xce
// SFR bit
#define SFR_PSW_CY  (SFR_PSW + 7)    // carry flag 用于无符号数运算 (ADD/ADDC/SUBB/RLC/RRC/SETB/CLR/JC/JNC)
#define SFR_PSW_AC  (SFR_PSW + 6)    // auxiliary carry flag. 低4位向高4位的进位,用于BCD码调整
#define SFR_PSW_F0  (SFR_PSW + 5)    // flag0
#define SFR_PSW_RS1 (SFR_PSW + 4)    // RS1-RS0 controls enables the working register of Rn
#define SFR_PSW_RS0 (SFR_PSW + 3)    // (0,0)=>(00h~07H);(0,1)=>(08F~0FH);(1,0)~(10H~1FH);(1,1)~(18H~1FH)
#define SFR_PSW_OV  (SFR_PSW + 2)    // overflow flag 用于有符号数运算
#define SFR_PSW_P   (SFR_PSW + 0)    // parity flag
#define SFR_SCON_TI (SFR_SCON + 1)
#define SFR_SCON_RI (SFR_SCON + 0)
// timer 1 overflow flag, clear and set by hardware. set on timer/counter overflow, clear on to interrupt routine
#define SFR_TCON_TF1 (SFR_TCON + 7)
// timer 1 run control bit. clear to turn off. set to turn on. timer/counter
#define SFR_TCON_TR1 (SFR_TCON + 6)
#define SFR_TCON_TF0 (SFR_TCON + 5)
#define SFR_TCON_TR0 (SFR_TCON + 4)
#define SFR_TCON_IE1 (SFR_TCON + 3)
#define SFR_TCON_IT1 (SFR_TCON + 2)
#define SFR_TCON_IE0 (SFR_TCON + 1)
#define SFR_TCON_IT0 (SFR_TCON + 0)
#define SFR_IE_EA    (SFR_IE + 7)
#define SFR_IE_ET2   (SFR_IE + 5)
#define SFR_IE_ES    (SFR_IE + 4)
#define SFR_IE_ET1   (SFR_IE + 3)
#define SFR_IE_EX1   (SFR_IE + 2)
#define SFR_IE_ET0   (SFR_IE + 1)
#define SFR_IE_EX0   (SFR_IE + 0)

#define INT_RESET_ENTRY  0x0000     // reset中断入口
#define INT_EXT0_ENTRY   0x0003     // 外部0中断入口
#define INT_TIMER0_ENTRY 0x000b     // timer0中断入口
#define INT_EXT1_ENTRY   0x00013    // 外部1中断入口
#define INT_TIMER1_ENTRY 0x001b     // timer1中断入口
#define INT_SERIAL_ENTRY 0x0023     // 串口中断入口
//数据存储区类型
typedef enum _mem_type_t
{
    MEM_TYPE_IRAM,    //内部ram,内部数据存储器,0x00~0x7f,128b
    MEM_TYPE_XRAM,    //外部存储器
    MEM_TYPE_CODE,    //内部存储器,0x0000~0xffff,64k
    MEM_TYPE_BIT,     //内部ram,0x20~0x2F,或者部分特殊功能寄存器:0x80~0xFF
    MEM_TYPE_SFR,     //特殊功能寄存器,0x80~0xff
} mem_type_t;
/**
 * @description: 读取一个字节
 * @param {mem_type_t} type
 * @param {uint16_t} addr
 * @return {*}
 */
uint8_t simu8051_read(mem_type_t type, uint16_t addr);
/**
 * @description: 写入一个字节
 * @param {mem_type_t} type
 * @param {uint16_t} addr
 * @param {uint8_t} data
 * @return {*}
 */
void simu8051_write(mem_type_t type, uint16_t addr, uint8_t data);

#endif